#region Using Directives

using System;
using System.IO;
using System.Windows.Forms;
using System.Globalization;
using Microsoft.VisualStudio.Modeling;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Library;
using Microsoft.Practices.RecipeFramework.Services;
using Microsoft.Practices.RecipeFramework.VisualStudio;
using Microsoft.Practices.RecipeFramework.VisualStudio.Templates;
using EnvDTE;
using EastBancTech.InstallerDefinition;
using Microsoft.Practices.Modeling.Dsl.Integration.Helpers;
using System.ServiceProcess;

#endregion

namespace EBT.Deployment.SoftwareFactory.Actions
{
    [ServiceDependency(typeof(DTE))]
    public class AddNewWindowsServiceToModel : AddComponentBase<WindowsService>
    {
        String serviceName;
        String serviceDisplayName;
        ServiceStartMode serviceStart;
        ServiceAccount serviceIdentity;
        String serviceFileName;

        [Input(Required = true)]
        public String ServiceName
        {
            get { return serviceName; }
            set { serviceName = value; }
        }
       

        [Input(Required = true)]
        public String ServiceDisplayName
        {
            get { return serviceDisplayName; }
            set { serviceDisplayName = value; }
        }
        

        [Input(Required = true)]
        public ServiceStartMode ServiceStart
        {
            get { return serviceStart; }
            set { serviceStart = value; }
        }
      

        [Input(Required = true)]
        public ServiceAccount ServiceIdentity
        {
            get { return serviceIdentity; }
            set { serviceIdentity = value; }
        }
      

        [Input(Required = true)]
        public String ServiceFileName
        {
            get { return serviceFileName; }
            set { serviceFileName = value; }
        }

        private ServiceStartType MapServiceStartMode2ServiceStartType(ServiceStartMode svcStart)
        {
            switch (svcStart)
            {
                case ServiceStartMode.Automatic:
                    return ServiceStartType.Auto;
                case ServiceStartMode.Disabled:
                    return ServiceStartType.Disable;
                case ServiceStartMode.Manual:
                    return ServiceStartType.Demand;
            }

            return ServiceStartType.Auto;
        }

        private IdentityType MapServiceAccount2IdentityType(ServiceAccount svcIdentity)
        {
            switch(svcIdentity)
            {
                case ServiceAccount.LocalService:
                    return IdentityType.localService;
                case ServiceAccount.LocalSystem:
                    return IdentityType.localSystem;
                case ServiceAccount.NetworkService:
                    return IdentityType.networkService;
                case ServiceAccount.User:
                    return IdentityType.other;
            }

            return IdentityType.other;
        }
      
        protected override LinkedElementCollection<WindowsService> LinkedCollection
        {
            get { return ComponentPackage.WindowsServices; }
        }

        protected override WindowsService CreateComponent(ComponentPackage cmpPackage)
        {
            WindowsService winSrvc = cmpPackage.Store.ElementFactory.CreateElement(WindowsService.DomainClassId) as WindowsService;
            winSrvc.Name = ServiceName;
            winSrvc.DisplayName = ServiceDisplayName;
            winSrvc.Description = ServiceDisplayName;
            winSrvc.Identity = MapServiceAccount2IdentityType(ServiceIdentity);
            winSrvc.ServiceFileName = ServiceFileName;
            winSrvc.Start = MapServiceStartMode2ServiceStartType(ServiceStart);
            winSrvc.Title = ServiceDisplayName;

            return winSrvc;
        }
    }
}
